<?php

namespace app\api\service;

use bgb\baidu\AipOcr;
use think\Db;
use think\Request;
use think\Validate;

class CommonService extends Api
{
    protected static $instance;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    public static function getInstance()
    {
        if (is_null(self::$instance))
        {
            self::$instance = new static();
        }
        return self::$instance;
    }

    /**
     * 识别营业执照
     */
    public function businessLicenseIdentification($params)
    {
        $Validate = new Validate([
            'businessLicense' => 'require'
        ], [
            'businessLicense' => '营业执照图片地址必传'
        ]);
        if (!$Validate->check($params))
        {
            $this->error($Validate->getError());
        }

        $client = new \bgb\orcIdcard\AipOcr('23495368', 'x29u3BjluojFSTeXDvzssIR7', 'aoOLtWWjHLpoQoyyOCNzbtV9mx7dDudG');
        $image = file_get_contents($params['businessLicense']);
        $idCardInfo = $client->businessLicense($image);
        return $idCardInfo;
    }

    /**
     * 识别身份证
     */
    public function idCardIdentification($params)
    {
        $Validate = new Validate([
            'idCard' => 'require',
            'idCardSide' => 'require',
        ], [
            'idCard' => '身份证图片地址必填',
            'idCardSide' => '身份证图片标识必填'
        ]);
        if (!$Validate->check($params))
        {
            $this->error($Validate->getError());
        }
        $cliend = new \bgb\orcIdcard\AipOcr('23495368', 'x29u3BjluojFSTeXDvzssIR7', 'aoOLtWWjHLpoQoyyOCNzbtV9mx7dDudG');
        $image = file_get_contents($params['idCard']);
        $idCardInfo = $cliend->idcard($image, $params['idCardSide']);
        return $idCardInfo;
    }

    /**
     * 获取法定节假日
     * 工作日为0 休息日为1 节假日为2
     */
    public function isHoliday($today)
    {
        if (cache($today) !== false) {
            return cache($today);
        } else {
            $api1 = file_get_contents('http://tool.bitefu.net/jiari/?d='.$today);
            if (is_numeric($api1)) {
                cache($today, $api1, 86400);
                return cache($today);
            } else {
                $api2 = json_decode(file_get_contents('http://www.easybots.cn/api/holiday.php?d='.$today));
                if (is_numeric($api2)) {
                    cache($today, $api2->$today, 86400);
                    return cache($today);
                } else {
                    return -1;
                }
            }
        }
    }

    /**
     * 处理多级数组结构
     */
    public function getTreeArr($data)
    {
        $result = [];
        foreach ($data as $k => $v)
        {
            foreach ($v as $kk => $vv)
            {
                if ($kk == 'child')
                {
                    $result[$k]['child'] = self::getTreeArr($v['child']);
                }else{
                    $result[$k][$kk] = $vv;
                }
            }
            if (!isset($result[$k]['child']))
            {
                unset($result[$k]);
            }
        }
        return $result;
    }

    /**
     * 获取地址库
     */
    public function getAreaList()
    {
        $area = Db::name('t_area')->select();
        $area = MenuService::getInstance()->getMenuTree($area, 0, 1);

        return $area;
    }

    /**
     * 获取地区名称
     */
    public function getAreaName($areaId)
    {
        return Db::name('t_area')->where([
            'id' => ['eq', $areaId]
        ])->value('name');
    }
}